<script setup>

import {Delete, Edit, Plus, Refresh, WarningFilled} from "@element-plus/icons-vue";
import {Search} from "@element-plus/icons-vue/global";
import {onMounted, reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import {addPayment, deletePayment, searchPayment, updatePayment} from "@/api/dormitoryApi/paymentApi.js";

const money = ref('')
const inputDormNum = ref('')
const inputArea = ref('')
const addFormVisible = ref(false)
const editFormVisible = ref(false)
const payments = ref([])
const addState = ref('未缴费')
const editState = ref('')
const currentPage = ref(1);
const total = ref(100);

const clear = () => {
  inputDormNum.value = '';
  inputArea.value = '';
  search();
}

const options = [
  {
    value: '已缴费',
    label: '已缴费',
  },
  {
    value: '未缴费',
    label: '未缴费',
  }
]

const addForm = reactive({
  dormNum: '',
  area: '',
  year: '',
  basic: '',
  water: '',
})
const editForm = reactive({
  dormNum: '',
  area: '',
  year: '',
  basic: '',
  water: '',
})

const edit = (moneyId,dormNum,area,state,year,basic,water) => {
  editFormVisible.value = true;
  money.value = moneyId;
  editForm.dormNum = dormNum;
  editForm.year = year;
  editForm.area = area;
  editForm.basic = basic;
  editForm.water = water;
  editState.value = state;
}

const search = () => {
  const searchParams = {
    limit: 10,
    page: currentPage.value,
    num: inputDormNum.value,
    name: inputArea.value,
  };

  searchPayment(searchParams).then(res => {
    if(res.status === 200){
      //查询成功
      payments.value = res.data.moneyIdVoList;
      total.value = res.data.total;
    }
  }).catch(() => {
    ElMessage.error("查询失败");
  })
}

const add = () => {
  const addParams = {
    dormNum: addForm.dormNum,
    basic: addForm.basic,
    area: addForm.area,
    state: addState.value,
    year: addForm.year,
    water: addForm.water,
  };

  addPayment(addParams).then(res => {
    if(res.status === 200){
      //添加成功
      ElMessage.success("添加成功");
      addFormVisible.value = false;
      search();
    }
  }).catch(() => {
    ElMessage.error("添加失败");
  })
}

const update = (moneyId,dormNum,area,state,year,basic,water) => {
  const updateParams = {
    moneyId: moneyId,
    dormNum: dormNum,
    area: area,
    state: state,
    year: year,
    basic: basic,
    water: water,
  };

  updatePayment(updateParams).then(res => {
    if(res.status === 200){
      //修改成功
      ElMessage.success("修改成功");
      editFormVisible.value = false;
      search();
    }
  }).catch(() => {
    ElMessage.error("修改失败");
  })
}

const deleteMessage = (moneyId) => {
  const deleteParams = {
    moneyId: moneyId,
  };

  deletePayment(deleteParams).then(res => {
    if(res.status === 200){
      //删除成功
      ElMessage.success("删除成功");
      search();
    }
  }).catch(() => {
    ElMessage.error("删除失败");
  })
}

const handleCurrentChange = (val) => {
  currentPage.value = val;
  search();
}

onMounted(() => {
  search();
})
</script>

<template>
  <div class="view-container">
    <div class="header">
      <el-card>
        <span style="font-size: large; color: rgb(51,133,217);">宿舍缴费信息管理</span>
        <div class="extra">
          <el-input v-model="inputDormNum" style="width: 200px" placeholder="输入宿舍号" />
          <el-input v-model="inputArea" style="width: 300px" placeholder="输入片区及楼栋名" />
          <el-button type="primary" plain @click="search"><el-icon><Search /></el-icon>搜索宿舍</el-button>
          <el-button @click="clear"><el-icon><Refresh /></el-icon>重置</el-button>
          <el-button type="primary" @click="addFormVisible = true"><el-icon><Plus /></el-icon>添加宿舍缴费信息</el-button>
        </div>
      </el-card>
    </div>

    <el-dialog v-model="addFormVisible" title="添加宿舍缴费信息" width="500">
      <el-form :model="addForm">
        <el-form-item label="宿舍号">
          <el-input v-model="addForm.dormNum" placeholder="请输入宿舍号" autocomplete="off" />
        </el-form-item>
        <el-form-item label="所属片区及楼栋">
          <el-input v-model="addForm.area" placeholder="请输入所属片区及楼栋" />
        </el-form-item>
        <el-form-item label="基础费用">
          <el-input v-model="addForm.basic" placeholder="请输入基础费用" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学年">
          <el-input v-model="addForm.year" placeholder="请输入学年" autocomplete="off" />
        </el-form-item>
        <el-form-item label="水电费">
          <el-input v-model="addForm.water" placeholder="请输入水电费" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="addState" placeholder="请选择状态">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click="add">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>

    <div class="table-container">
      <el-table :data="payments" style="width: 100%">
        <el-table-column label="序号" prop="suiId" align="center" width="120"></el-table-column>
        <el-table-column label="宿舍号" prop="dormNum" align="center" width="100"></el-table-column>
        <el-table-column label="所属片区及楼栋" prop="area"></el-table-column>
        <el-table-column label="学年" prop="year"></el-table-column>
        <el-table-column label="基础费用" prop="basic" align="center" width="110"></el-table-column>
        <el-table-column label="水电费" prop="water" align="center" width="140"></el-table-column>
        <el-table-column label="状态" prop="state" align="center" width="120"></el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template #default="{ row }">
            <el-button :icon="Edit" circle plain type="primary" @click="edit(row.moneyId,row.dormNum,row.area,row.state,row.year,row.basic,row.water)"></el-button>
            <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="WarningFilled"
                icon-color="#ffcc00"
                title="确定删除此条信息？"
                @confirm="deleteMessage(row.moneyId)"
            >
              <template #reference>
                <el-button :icon="Delete" circle plain type="danger"></el-button>
              </template>
            </el-popconfirm>
            <el-dialog v-model="editFormVisible" title="修改宿舍缴费信息" width="500">
              <el-form :model="editForm">
                <el-form-item label="宿舍号">
                  <el-input v-model="editForm.dormNum" placeholder="请输入宿舍号" autocomplete="off" />
                </el-form-item>
                <el-form-item label="所属片区及楼栋">
                  <el-input v-model="editForm.area" placeholder="请输入所属片区及楼栋" />
                </el-form-item>
                <el-form-item label="基础费用">
                  <el-input v-model="editForm.basic" placeholder="请输入基础费用" autocomplete="off" />
                </el-form-item>
                <el-form-item label="学年">
                  <el-input v-model="editForm.year" placeholder="请输入学年" autocomplete="off" />
                </el-form-item>
                <el-form-item label="水电费">
                  <el-input v-model="editForm.water" placeholder="请输入水电费" autocomplete="off" />
                </el-form-item>
                <el-form-item label="状态">
                  <el-select v-model="editState" placeholder="请选择状态">
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-form>
              <template #footer>
                <div class="dialog-footer">
                  <el-button @click="editFormVisible = false">取消</el-button>
                  <el-button type="primary" @click="update(money,editForm.dormNum,editForm.area,editState,editForm.year,editForm.basic,editForm.water)">
                    确定
                  </el-button>
                </div>
              </template>
            </el-dialog>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="暂无数据" />
        </template>
      </el-table>
    </div>
    <div class="container-bottom">
      <el-pagination
          v-model:current-page="currentPage"
          page-size="10"
          :background="true"
          layout="total, prev, pager, next"
          :total="total"
          style="margin-top: 30px; justify-content: center"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.view-container{
  overflow: hidden;
  margin-top: 30px;
  margin-left: 300px;
  margin-right: 50px;
}
.table-container{
  margin-top: 20px;
}
.extra{
  margin-top: 20px;
  display: flex;
  gap: 30px;
}
</style>
